package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"go-go-go-sys/cache"
	"go-go-go-sys/models"
	"strconv"
	"time"
)

func RoleNode(c *gin.Context) {
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	nid, _ := strconv.Atoi(c.PostForm("nid"))

	data := models.RoleNode{
		Rid: rid,
		Nid: nid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func RoleList(c *gin.Context) {
	uid := c.Query("uid")
	var news []models.Role
	/*
		SELECT
		  *
		FROM
		  roles
		  LEFT JOIN user_roles ON user_roles.rid = roles.id
		WHERE
		  user_roles.uid = 1
	*/
	models.DB.Table("roles").
		Joins("LEFT JOIN user_roles ON user_roles.rid = roles.id").
		Where("user_roles.uid = ?", uid).
		Debug().
		Find(&news)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": news,
	})
	return
}

func NodeList(c *gin.Context) {
	nid := c.Query("nid")
	var news []models.Node
	/*
		SELECT
		  *
		FROM
		  nodes
		  LEFT JOIN role_nodes ON role_nodes.rid = nodes.id
		WHERE
		  role_nodes.nid = 1
	*/
	models.DB.Table("nodes").
		Joins("LEFT JOIN role_nodes ON role_nodes.rid = nodes.id").
		Where("role_nodes.nid = ?", nid).
		Debug().
		Find(&news)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": news,
	})
	return

}

func CarAdd(c *gin.Context) {
	carName := c.PostForm("carName")
	carType := c.PostForm("carType")
	carPrice := c.PostForm("carPrice")

	data := models.Car{
		CarName:  carName,
		CarType:  carType,
		CarPrice: carPrice,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func OrderAdd(c *gin.Context) {
	orderCode := c.PostForm("orderCode")
	fromAddress := c.PostForm("fromAddress")
	toAddress := c.PostForm("toAddress")
	cid, _ := strconv.Atoi(c.PostForm("cid"))

	data := models.Order{
		OrderCode:   orderCode,
		FromAddress: fromAddress,
		ToAddress:   toAddress,
		Cid:         cid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func OrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "2"))

	offset := (page - 1) * size

	var news []models.CarOrderList

	redis_key := "list_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("orders").
			Select(
				"orders.id as OrderId",
				"cars.car_name as CarName",
				"cars.car_type as CarType",
				"cars.car_price as CarPrice",
				"orders.order_code as OrderCode",
				"orders.from_address as FromAddress",
				"orders.to_address as ToAddress",
			).
			Joins(" LEFT JOIN cars ON cars.id = orders.id").
			Offset(offset).
			Limit(size).
			Find(&news)
		jsonData, _ := json.Marshal(news)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &news)
	}
	/*
		SELECT
		  orders.id as OrderId,
		  cars.car_name as CarName,
		  cars.car_type as CarType,
		  cars.car_type as CarPrice,
		  orders.order_code as OrderCode,
		  orders.from_address as FromAddress,
		  orders.to_address as ToAddress
		FROM
		  orders
		  LEFT JOIN cars ON cars.id = orders.id
	*/

	var count int64
	models.DB.Model(&models.Order{}).Count(&count)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"data":  news,
		"count": count,
	})
	return
}

func OrderDetail(c *gin.Context) {
	id := c.Query("id")

	var news models.CarOrderList
	redis_key := "detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("orders").
			Select(
				"orders.id as OrderId",
				"cars.car_name as CarName",
				"cars.car_type as CarType",
				"cars.car_price as CarPrice",
				"orders.order_code as OrderCode",
				"orders.from_address as FromAddress",
				"orders.to_address as ToAddress",
			).
			Joins(" LEFT JOIN cars ON cars.id = orders.id").
			Where("orders.id =?", id).
			Find(&news)
		jsonData, _ := json.Marshal(&news)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &news)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": news,
	})
	return
}
